DECLARE FUNCTION setupSound AS UByte
DECLARE FUNCTION setupDifficulty AS UByte

SUB showLogo()
'para otra vez mejor evitar \" esos gdus

initScreen()


PRINT AT 17,10;LOGO_PRESENT

INK 6
PRINT AT 1,8;"______ __  |# $@";
PRINT AT 2,7;"|     |_@$$[[@#\"
PRINT AT 3,7;"|  $\# @\]#| \ \"
PRINT AT 4,7;"| $ \ #@\[$ [\ \"
PRINT AT 5,7;"|$ $\# @\[#| \ \"
PRINT AT 6,7;"|# #\$|#_@$ [\#|"; AT 7,7;"| # \ $|#[]]$]$[#"; AT 8,7;"| $ \ #| @\][#@\|"; AT 9,7;"|$ $ #_#@[\| $@\|"; AT 10,7;"|]]   |]@@\ [#@\|"; AT 11,8;"]]]]]]$ ]\ \ #]$"; AT 12,14;"]]]]] ]]]"; AT 13,17;CREDITS_L4;

pausaIntro()
  mainMenu()
   
   
END SUB
SUB intro()

INK 5
#IFDEF ENGLISH_LANG
        printTextXY(INTRO_L1,3,1) 
        printTextXY(INTRO_L2,5,4)
        printTextXY(INTRO_L3,8,1) 
        printTextXY(INTRO_L4,11,1) 
        printTextXY(INTRO_L5,12,2) 
        printTextXY(INTRO_L6,15,3)
#ELSE
        printTextXY(INTRO_L1,3,1) 
        printTextXY(INTRO_L2,5,4)
        printTextXY(INTRO_L3,8,1) 
        printTextXY(INTRO_L4,11,2) 
        printTextXY(INTRO_L5,12,4) 
        printTextXY(INTRO_L6,15,3)
#ENDIF


INK 7
PRINT AT 19,6; LOGO_TITLE;

pausaIntro() 
pressAnyKey()

END SUB
SUB mainMenu()


do
initScreen()



PRINT AT 3,6; LOGO_TITLE;
INK 6
PRINT AT 7,8;"1.";MENU_STARTGAME; AT 9,8;"2.";MENU_CONTROLS; AT 11,8;"3.";MENU_DIFFICULTY; 
INK 5

#IFDEF ENGLISH_LANG
        PRINT AT 18,9;MENU_DIFFICULTY_SCORE;
#ELSE
        PRINT AT 18,5;MENU_DIFFICULTY_SCORE;
#ENDIF

INK 7
PRINT maxScore;
 
  printPlayer(3,7,0)
     menuWaitNoKey()
     DO

        keyString=INKEY
         
     LOOP UNTIL (keyString="1") OR (keyString="2") OR (keyString="3") 'OR (keyString="4")

   respuesta = CAST(UBYTE, VAL(keyString))
   
   
   CLS
 
  printPlayer(3,7,0)
   IF respuesta = 2 then controllerType=setupController()
   IF respuesta=3 then difficulty=setupDifficulty()
  


 LOOP until respuesta=1
 
 IF respuesta=1 then
        RANDOMIZE   
       
       initScreen()
       intro()
       new_game()
       next_room()
       
 END IF
 
END SUB

FUNCTION setupDifficulty() AS UBYTE
 DIM a AS UBYTE 
   DIM key AS STRING
    

PRINT AT 3,10;MENU_DIFFICULTY;
colorMenu(difficulty,0)
PRINT AT 7,8;MENU_DIFFICULTY_EASY;
colorMenu(difficulty,1)
PRINT AT 9,8;MENU_DIFFICULTY_NORMAL;
colorMenu(difficulty,2)
PRINT AT 11,8;MENU_DIFFICULTY_HARD;
 
    menuWaitNoKey()
     DO
 
         key=INKEY
         
     LOOP UNTIL (key="1") OR (key="2") OR (key="3") 

   a= (CAST(UBYTE, VAL(key)))-1
   
   
   RETURN a
END FUNCTION
SUB colorMenu(var AS UBYTE ,a AS UBYTE)

IF var=a THEN 
       INK 4
ELSE 
    INK 6
END IF
END SUB
SUB initScreen()
    
    BRIGHT 1
    INK 7
    CLS
    
END SUB
SUB WipeScreen()
'borrado de pantalla
DIM y AS BYTE
    DIM centro AS BYTE
    DIM offset AS BYTE
    DIM i AS BYTE

    centro = 9   ' Centro ajustado: 24 - 4 = 20, mitad de 0–19 = 9.5 → usamos 9
    
    FOR offset = 0 TO centro
        ' Línea superior
        IF (centro - offset) >= 0 THEN
            PRINT AT (centro - offset), 0;
            FOR i = 1 TO 32 
                PRINT " "; 
            NEXT i
        END IF

        ' Línea inferior
        IF (centro + offset) <= 19 THEN  ' Limitar hasta línea 19
            PRINT AT (centro + offset), 0;
            FOR i = 1 TO 32 
                PRINT " "; 
            NEXT i
        END IF

        PAUSE 1
    NEXT offset
END SUB
SUB show_hp()

    DIM y AS UBYTE
    y=5
    INK 2
    BRIGHT 1
    'LIMPIADO DE HP
    FOR x=1 TO 4
               PRINT at y,29;"  "; at y+1,29;"  ";
               IF (player_hp-1)>= x then PutChars(29,y,2,2,@hudTiles_vida(0))               
               y=y+3          
    NEXT x           

END SUB
SUB print_timebar()
    DIM barra AS integer
 
    
    barra=time_left
    barra=INT((barra*10)/time_total) 'regla de 3, para obtener de 0 a 10
    'barraAnterior es global(si fuera local cada vez que entra a la rutina se reseteria)
    IF barraAnterior<>barra then
        
        barraAnterior=barra
        INK 1
        FOR y=0 TO 9
             'imprime la barra de 3,1 hasta 13,1
                IF barra>=y then 
                       BRIGHT 1
                       PRINT at 15-y,1;"  "; 
                       '16 porque asi queda de forma ascendente de y=6 a y=15
                
                
                            ' Imprimimos el sprite/tile
                             PutChars(1,15-y,1,1,@hudTiles_tiempo(0,0))
                             PutChars(2,15-y,1,1,@hudTiles_tiempo(1,0))
                ELSE

                            PRINT at 15-y,1;"  ";
                END IF
        NEXT y
        
    END IF
END SUB
SUB printLlave()
    INK 6
    BRIGHT 1
    PRINT at 16,26;"  "; at 17,26;"  ";
    PutChars(26,16,2,2,@hudTiles_llave(0))
                            
END SUB
SUB printMarco()
    DIM j AS UBYTE
    
    'Rellenado de color
    INK 5
    BRIGHT 1
    FOR y=19 TO 23    
         PRINT at y,0;" "; at y,31;" ";
    NEXT y
    FOR x=1 to 30
         BRIGHT 1
         PRINT AT 19,x;" ";
         BRIGHT 0
         PRINT AT 23,x;" ";  
    NEXT x
    'marco lateral izquierdo
    putChars(0,19,1,1,@hudTiles_frame(0,0))   
    FOR x=2 TO 5
            putChars(0,18+x,1,1,@hudTiles_frame(x*2,0)) '4,6,8,10
    NEXT x
     'marco lateral derecho
    FOR x=2 TO 5   '2 3 4 5
            putChars(31,17+x,1,1,@hudTiles_frame((x*2)-1,0)) '3,5,7,9
    NEXT x
    putChars(31,23,1,1,@hudTiles_frame(13,0))   
    
    'marco superior e inferior
    FOR x=1 TO 30
            j=(INT(RND*2))+1
            putChars(x,19,1,1,@hudTiles_frame(j,0)) 
            putChars(x,23,1,1,@hudTiles_frame(j+10,0)) 
    NEXT x    
END SUB

SUB show_hud()
    
 DIM y AS ubyte
    'llave
            
    IF llave=1 then printLlave()
             
    
    'hud tiempo

    INK 6
    BRIGHT 0
    PRINT at 5,1;" "; at 16,1;" ";
    BRIGHT 1
    PRINT at 5,2;" "; at 16,2;" "; 


    putChars(1,5,1,1,@hudTiles_adorno(0,0)) 
    putChars(2,5,1,1,@hudTiles_adorno(1,0)) 
    putChars(1,16,1,1,@hudTiles_adorno(2,0)) 
    putChars(2,16,1,1,@hudTiles_adorno(3,0))    
   
   
   'reset de barra anterior para que pinte la barra almenos 1 vez al volver de otra pantalla
   barraAnterior=0
   print_timebar() 
   show_hp()
   
END sUB
SUB printChest(x as UBYTE,y as ubyte)
  
            
            putCharsOverMode(x,y,2,2,2,@tiles_chest_Mask(0))
            putCharsOverMode(x,y,2,2,3,@tiles_chest(0))
           
   
END SUB
SUB printInput()
'dibuja el interrogante cuando espera tecla del jugador
INK  5
BRIGHT 1
PRINT at 9,15;"  "; at 10,15;"  ";

PutChars(15,9,2,2,@hudTiles_input(0))
END SUB
SUB eraseInput()
PRINT at 9,15;"  "; at 10,15;"  ";

END SUB
SUB printLock()
    PutChars(21,6,2,3,@hudTiles_lock(0))
END SUB
SUB printEvent(type as ubyte)
    
     'Recoloca player para recibir evento o esperar Input
     reprintSpot()
     printPlayer(14,11,0)
    
    IF map_events(pl_x,pl_y)<6 AND map_events(pl_x,pl_y)>0
            BRIGHT 1

            IF type=1
                   INK 6  
                   eraseInput()
                   PutChars(15,9,2,2,@hudTiles_llave(0))
            ELSE IF type=2
                   INK 2  
                   eraseInput()
                   PutChars(15,9,2,2,@hudTiles_vida(0))

            ELSE IF type=3
                   INK 7  
                   eraseInput()
                   PutChars(15,9,2,2,@hudTiles_events(0,0))

            ELSE IF type=4
                   
                   INK 4  
                   eraseInput()
                   PutChars(15,9,2,2,@hudTiles_events(1,0))
                   
            ELSE IF type=5
                   
                   INK 1  
                   eraseInput()
                   INK 4                     
                   PRINT AT 10,15;"  ";
                   PutChars(15,9,2,2,@hudTiles_events(2,0))  
                   
            END IF
            
     END IF 
 
END SUB
SUB printDoorD()
 'puertas Down
waitretrace
 putCharsOverMode(7,9,5,9,2,@tiles_puertaD_Mask(0))
 putCharsOverMode(7,9,5,9,3,@tiles_puertaD(0))
END SUB
SUB printDoorR()
 'puertas Right
 waitretrace
 putCharsOverMode(20,9,5,9,2,@tiles_puertaR_Mask(0))
 putCharsOverMode(20,9,5,9,3,@tiles_puertaR(0)) 
END SUB
SUB printPlayer(x as UBYTE,y as ubyte, frame as ubyte)
 waitretrace
 DIM mask,pic AS Uinteger
 IF playerDir=1 OR playerFrom=1 
           mask=@tiles_playerL_Mask(frame,0)
           pic=@tiles_playerL(frame,0)
 ELSE If playerDir=2 OR playerFrom=2
           mask=@tiles_playerU_Mask(frame,0)
           pic=@tiles_playerU(frame,0)
 ELSE If playerDir=4 OR playerFrom=4
         mask=@tiles_playerD_Mask(frame,0)
          pic=@tiles_playerD(frame,0)
 ELSE 
          mask=@tiles_playerR_Mask(frame,0)
          pic=@tiles_playerR(frame,0)
 END IF
  
           putCharsOverMode(x,y,4,4,2,mask)
           putCharsOverMode(x,y,4,4,3,pic)
END SUB
SUB printRoom()
  'funcion que pinta toda la sala estilo por tiles leyendo la matriz map_pool
  
  room=map_pool(current_map,pl_x,pl_y)
 
 BRIGHT 0
 
 IF level=0 then INK 7
 IF level=1 then INK 4
 IF level=2 then INK 3
 IF level=3 then INK 6
 IF level=4 then INK 2
 
 WipeScreen()
 

 
 ' Variables locales
 ' Para el bucle de dibujado
 DIM x, y AS UByte
 ' Posición del tile en pantalla
 DIM tx, ty AS UByte
 ' Dirección del próximo tile en el mapa

 ' Número de tile a dibujar
 DIM tile AS UByte

 ' Posición inicial Y en pantalla: 0
 ty = 4
 ' Recorremos las filas de 0 a 11
 FOR y = 0 TO 11
 ' Posición inicial X en pantalla: 0
 tx = 2
 ' Recorremos las columnas de 0 a 7
 FOR x = 0 TO 7
 ' Obtenemos el número de tile a dibujar
 tile = roomTiles(room-1,y,x) 
 ' Imprimimos el sprite/tile
 PutChars(ty,tx,2,2,@tiles_rooms(tile-1,0))

 ' Siguiente columna (posición X) en pantalla
 tx = tx + 2
 NEXT x
 ' Siguiente fila (posición Y) en pantalla
 ty = ty + 2
 NEXT y

'DIBUJA PUERTAS
IF walls(room,2)>=1 then printDoorR()
IF walls(room,3)>=1 then printDoorD()
IF room=17 then printLock()
END SUB
SUB reprintSpot()
  'funcion para volver a dibujar el suelo para tapar el jugador de cara a el siguiente paso cuando se mueve
  
  room=map_pool(current_map,pl_x,pl_y)

 DIM x, y AS UByte
 DIM tx, ty AS UByte
 DIM tile AS UByte

ty=plSubX

x= (plSubY-2) /2
y=(plSubX-4) /2

        IF (plSubY MOD 2)=0 then
                                tx=plSubY
        ELSE 
                                 tx=plSubY-1
        END if

                       
                       
                       tile = roomTiles(room-1,y,x) 
                      PutChars(ty,tx,2,2,@tiles_rooms(tile-1,0))  
                      tile = roomTiles(room-1,y+1,x) 
                      PutChars(ty+2,tx,2,2,@tiles_rooms(tile-1,0))
                      tile = roomTiles(room-1,y,x+1)                     
                      PutChars(ty,tx+2,2,2,@tiles_rooms(tile-1,0))
                      tile = roomTiles(room-1,y+1,x+1)                          
                      PutChars(ty+2,tx+2,2,2,@tiles_rooms(tile-1,0))
    
 
 IF (plSubY MOD 2)=0 then

    ELSE                
                    
                  
                   

                    
                    tile = roomTiles(room-1,y,x+2) 
                    PutChars(ty,tx+4,2,2,@tiles_rooms(tile-1,0))
                    tile = roomTiles(room-1,y+1,x+2) 
                    PutChars(ty+2,tx+4,2,2,@tiles_rooms(tile-1,0)) 
    END IF



END SUB
SUB printBoss()
 
   PaintData(9,4,15,15,@tiles_boss1_Attr(0))
   PutChars(9,4,15,15,@tiles_boss1(0))

  
END SUB
SUB printTextXY(mensaje$ AS STRING, startY AS UBYTE, startX AS UBYTE)
    'ProntTextXY("texto",x,y,slow). subrutina que imprime el texto que pongas entre comillas "hola mundo", en la posicion x,y y además lo hace graducalmente
    
    DIM i AS UBYTE
    DIM x AS UBYTE
    DIM y AS UBYTE
    DIM limite AS UBYTE

    x = startX          ' Comienzo de columna
    y = startY          ' Comienzo de fila
    limite = 30         ' Ancho útil 

    FOR i = 1 TO LEN(mensaje$)
        PRINT AT y, x; mensaje$(i - 1);
        x = x + 1

        IF x > (startX + limite - 1) THEN
            y = y + 1
            x = startX
        END IF

    PAUSE 1
    NEXT i
END SUB
SUB printLevel()
    INK 7
    BRIGHT 1
    PRINT AT 1,1;HUD_LEVEL;level+1;

END SUB
SUB printScore()

PRINT AT 1,25;HUD_SCORE;AT 3,26;"00000";
 
IF score <10 

        PRINT AT 3,30;score;  
        
ELSE IF score <100 

        PRINT AT 3,29;score; 
        
ELSE IF score <1000 

        PRINT AT 3,28;score; 
        
ELSE IF score <10000 
        
        PRINT AT 3,27;score; 
ELSE
        PRINT AT 3,26;score; 
END IF        
END SUB
SUB limpia_texto()
 '
 FOR x=1 TO 30
        PRINT at 20,x; " "; at 21,x; " "; at 22,x; " ";      
 NEXT x
 
 END SUB
SUB levelClear()
        
        'aqui multiplicamos "difficulty" (que es ubyte 0 a 255) por 150, con lo que no podría funcionar con ubyte. Una solucion (en lugar de traspasar a una variable integer), sería usar CAST(tipo de variable quieres, variable en cuestion)
        
        initScreen()       
       
        PRINT AT 5,8; HUD_LEVEL;level+1;HUD_LV_CLEAR; AT 9,8; HUD_SCORE;HUD_LEVEL;score; AT 11,8; HUD_CLEAR_BONUS;time_left + (CAST(uinteger, difficulty) * 150) ;
        score = score + time_left + (CAST(uinteger, difficulty) * 150)   
        PRINT AT 17,8; HUD_TOTAL;HUD_LEVEL;score;  
                
                
        'NOTAS: MAX TOTAL PUNTOS NIVEL 6500. 
        ' RESTAN 3500 PUNTOS A REPARTIR EN 5 NIVELES= """"700"""" COMO BONUS MAXIMO POR TIEMPO POR NIVEL
        
        playSFX7()
        
        pressAnyKey()
        
        IF level<4 then 
                
                nextLevel()
                
        ELSE 
                gameCompleted()
                
        END IF
        
END SUB
SUB showNextLevel()

initScreen()
PRINT AT 8,6;LEVEL_READY; AT 12,12; HUD_LEVEL;level+1;
playSFX2()
printMarco()

END SUB
SUB gameover()
    initScreen()
    
    PAUSE 15
    
    printBoss()
    
    PRINT AT 2,7; LEVEL_GAMEOVER;
    IF time_left=0 then printTextXY(LEVEL_GAMEOVER_TIME,20,3)
    IF player_hp<=1 then printTextXY(LEVEL_GAMEOVER_LIVES,20,4)    
    playSFX6()   
    endGame=1
    pressAnyKey()
    
    
END SUB
SUB gameCompleted()
initScreen()

#IFDEF ENGLISH_LANG
    PRINT AT 1, 2;GAME_COMPLETED1; AT 5, 4;GAME_COMPLETED2;AT 6, 8;GAME_COMPLETED3;AT 8, 5;GAME_COMPLETED4;AT 9, 4;GAME_COMPLETED5;AT 11,3;GAME_COMPLETED6;AT 12, 5;GAME_COMPLETED7;AT 13, 5;GAME_COMPLETED8;AT 16, 6;GAME_COMPLETED9; AT 18, 5;GAME_COMPLETED10; AT 19, 2;GAME_COMPLETED11;

#ELSE
    PRINT AT 1, 4;GAME_COMPLETED1; AT 5, 4;GAME_COMPLETED2;AT 6, 8;GAME_COMPLETED3;AT 8, 3;GAME_COMPLETED4;AT 9, 6;GAME_COMPLETED5;AT 11,4;GAME_COMPLETED6;AT 12, 5;GAME_COMPLETED7;AT 13, 3;GAME_COMPLETED8;AT 16, 7;GAME_COMPLETED9; AT 18, 1;GAME_COMPLETED10; AT 19, 4;GAME_COMPLETED11;

#ENDIF
printChest(14,2)

endGame=1
pressAnyKey()


END SUB
SUB credits()

initScreen()

PRINT at 1,6;CREDITS_L1; at 2,6;CREDITS_L2;GAME_COMPLETED9;CREDITS_L2; at 3,6;CREDITS_L1; at 6,5;CREDITS_L3; at 7,12;CREDITS_L4; at 9,4;CREDITS_L5; at 12,7;CREDITS_L6; at 14,9;CREDITS_L7; at 15,9;CREDITS_L8; at 16,9;CREDITS_L9; at 17,9;CREDITS_L10; at 19,4;CREDITS_L11;


pressAnyKey()

END SUB